package com.ibeeking.found.msg.rest.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ibeeking.found.msg.api.common.dto.KfServicerDTO;
import com.ibeeking.found.msg.api.feign.MsgCopBFeignClient;
import com.ibeeking.found.msg.rest.service.IOpenService;
import com.ibeeking.found.msg.rest.service.ISendWxCpMsgService;
import com.ibeeking.found.msg.rest.service.IWxCpMsgTemplateService;
import com.ibeeking.found.msg.service.common.vo.SysMsgTemplateVO;
import com.ibeeking.found.msg.service.common.vo.WxCpMsgTemplateVO;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpKfService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgSendRequest;
import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfTextMsg;
import org.apache.commons.lang.text.StrSubstitutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SendSmsServiceImpl
 * @Description 发送短信实现类
 * @Author ibeeking
 * @Date 2020-12-10 9:21
 **/
@Service
public class SendWxCpMsgServiceImpl implements ISendWxCpMsgService {

    @Resource
    private IOpenService wxOpenServiceImpl;

    @Resource
    private MsgCopBFeignClient msgCopBFeignClient;

    @Resource
    private IWxCpMsgTemplateService wxCpMsgTemplateServiceImpl;


    @Override
    public Boolean sendWxCpTemplateMessage(Integer tenantId, String templateCode, List<String> toUser, Map<String, String> paramMap) throws WxErrorException {
        if (StringUtils.isEmpty(templateCode) || CollUtil.isEmpty(paramMap) || Collections3.isEmpty(toUser)) {
            return false;
        }
        WxCpMsgTemplateVO wxCpMsgTemplateVO = wxCpMsgTemplateServiceImpl.queryTemplateByCode(templateCode);
        if (ObjectUtil.isNull(wxCpMsgTemplateVO)) {
            throw new BusinessException("企业微信消息模板查询失败 templateId:" + templateCode);
        }

        WxCpService wxCpService = wxOpenServiceImpl.getWxCpService(Long.valueOf(tenantId));
        WxCpKfService msgService = wxCpService.getKfService();
        WxCpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCpKfMsgSendRequest();
        ResponseResult<KfServicerDTO> kfServicerDTOResult = msgCopBFeignClient.queryDefaultKfServicer();
        if (!ResponseCodeEnum.SUCCESS.getCode().equals(kfServicerDTOResult.getCode())) {
            throw new BusinessException("没有查到主客服账号，请确认是否设置了主客服账号");
        }
        String templateContent = wxCpMsgTemplateVO.getContent();
        if(StringUtils.isEmpty(templateContent)) throw new BusinessException("模板内容为空！");
        if(paramMap!=null && paramMap.size()>0) {
            // 组装模板内容 模板格式参考 "<p>您好，${name}，您的工号是${idNumber},您的年龄是${age}</p>";
            StrSubstitutor strSubstitutor = new StrSubstitutor(paramMap);
            String content = strSubstitutor.replace(templateContent);
            WxCpKfTextMsg textMsg = new WxCpKfTextMsg();
            textMsg.setContent(content);
            wxCpKfMsgSendRequest.setText(textMsg);
        }
        KfServicerDTO kfServicerDTO = kfServicerDTOResult.getData();
        wxCpKfMsgSendRequest.setOpenKfid(kfServicerDTO.getOpenKfid());
        wxCpKfMsgSendRequest.setToUser(kfServicerDTO.getEmployeeCode());
        wxCpKfMsgSendRequest.setMsgType(WxConsts.KefuMsgType.TEXT);
        msgService.sendMsg(wxCpKfMsgSendRequest);
        return true;
    }

}
